package com.pushtime.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.pushtime.service.HdfsLogService;
import com.pushtime.web.rest.errors.BadRequestAlertException;
import com.pushtime.web.rest.util.HeaderUtil;
import com.pushtime.web.rest.util.PaginationUtil;
import com.pushtime.service.dto.HdfsLogDTO;
import com.pushtime.service.dto.HdfsLogCriteria;
import com.pushtime.service.HdfsLogQueryService;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing HdfsLog.
 */
@RestController
@RequestMapping("/api")
public class HdfsLogResource {

    private final Logger log = LoggerFactory.getLogger(HdfsLogResource.class);

    private static final String ENTITY_NAME = "hdfsutilHdfsLog";

    private final HdfsLogService hdfsLogService;

    private final HdfsLogQueryService hdfsLogQueryService;

    public HdfsLogResource(HdfsLogService hdfsLogService, HdfsLogQueryService hdfsLogQueryService) {
        this.hdfsLogService = hdfsLogService;
        this.hdfsLogQueryService = hdfsLogQueryService;
    }

    /**
     * POST  /hdfs-logs : Create a new hdfsLog.
     *
     * @param hdfsLogDTO the hdfsLogDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new hdfsLogDTO, or with status 400 (Bad Request) if the hdfsLog has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/hdfs-logs")
    @Timed
    public ResponseEntity<HdfsLogDTO> createHdfsLog(@Valid @RequestBody HdfsLogDTO hdfsLogDTO) throws URISyntaxException {
        log.debug("REST request to save HdfsLog : {}", hdfsLogDTO);
        if (hdfsLogDTO.getId() != null) {
            throw new BadRequestAlertException("A new hdfsLog cannot already have an ID", ENTITY_NAME, "idexists");
        }
        HdfsLogDTO result = hdfsLogService.save(hdfsLogDTO);
        return ResponseEntity.created(new URI("/api/hdfs-logs/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /hdfs-logs : Updates an existing hdfsLog.
     *
     * @param hdfsLogDTO the hdfsLogDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated hdfsLogDTO,
     * or with status 400 (Bad Request) if the hdfsLogDTO is not valid,
     * or with status 500 (Internal Server Error) if the hdfsLogDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/hdfs-logs")
    @Timed
    public ResponseEntity<HdfsLogDTO> updateHdfsLog(@Valid @RequestBody HdfsLogDTO hdfsLogDTO) throws URISyntaxException {
        log.debug("REST request to update HdfsLog : {}", hdfsLogDTO);
        if (hdfsLogDTO.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        HdfsLogDTO result = hdfsLogService.save(hdfsLogDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, hdfsLogDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /hdfs-logs : get all the hdfsLogs.
     *
     * @param pageable the pagination information
     * @param criteria the criterias which the requested entities should match
     * @return the ResponseEntity with status 200 (OK) and the list of hdfsLogs in body
     */
    @GetMapping("/hdfs-logs")
    @Timed
    public ResponseEntity<List<HdfsLogDTO>> getAllHdfsLogs(HdfsLogCriteria criteria, Pageable pageable) {
        log.debug("REST request to get HdfsLogs by criteria: {}", criteria);
        Page<HdfsLogDTO> page = hdfsLogQueryService.findByCriteria(criteria, pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/hdfs-logs");
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

    /**
    * GET  /hdfs-logs/count : count all the hdfsLogs.
    *
    * @param criteria the criterias which the requested entities should match
    * @return the ResponseEntity with status 200 (OK) and the count in body
    */
    @GetMapping("/hdfs-logs/count")
    @Timed
    public ResponseEntity<Long> countHdfsLogs(HdfsLogCriteria criteria) {
        log.debug("REST request to count HdfsLogs by criteria: {}", criteria);
        return ResponseEntity.ok().body(hdfsLogQueryService.countByCriteria(criteria));
    }

    /**
     * GET  /hdfs-logs/:id : get the "id" hdfsLog.
     *
     * @param id the id of the hdfsLogDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the hdfsLogDTO, or with status 404 (Not Found)
     */
    @GetMapping("/hdfs-logs/{id}")
    @Timed
    public ResponseEntity<HdfsLogDTO> getHdfsLog(@PathVariable Long id) {
        log.debug("REST request to get HdfsLog : {}", id);
        Optional<HdfsLogDTO> hdfsLogDTO = hdfsLogService.findOne(id);
        return ResponseUtil.wrapOrNotFound(hdfsLogDTO);
    }

    /**
     * DELETE  /hdfs-logs/:id : delete the "id" hdfsLog.
     *
     * @param id the id of the hdfsLogDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/hdfs-logs/{id}")
    @Timed
    public ResponseEntity<Void> deleteHdfsLog(@PathVariable Long id) {
        log.debug("REST request to delete HdfsLog : {}", id);
        hdfsLogService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
}
